package com.liujit.standard.basis.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liujit.standard.basis.admin.model.SysMenu;
import com.liujit.standard.basis.admin.service.SysMenuService;
import com.liujit.standard.basis.core.util.CoreUtil;
import com.liujit.standard.basis.admin.config.annotation.Auth;
import com.liujit.standard.basis.admin.config.annotation.Log;
import com.liujit.standard.basis.admin.domain.query.SysMenuQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 系统菜单管理
 * @Author LiuJun
 * @Date 2021-03-26 09:15:51
 */
@Api(value = "系统菜单管理接口", tags = "系统菜单管理接口")
@EnableTransactionManagement
@RestController
@RequestMapping("/admin/sys/menu")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @Auth("sysMenu-sel")
    @ApiOperation(value = "查询列表", notes = "查询列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysMenuQuery")})
    @GetMapping("/getList")
    public List<SysMenu> getList(SysMenuQuery query) {
        CoreUtil.Paging(query, "seq, create_time desc");
        QueryWrapper wrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(query.getTitle())) {
            wrapper.like("title", query.getTitle());
        }
        if (query.getPid() != null) {
            wrapper.eq("pid", query.getPid());
        }
        return sysMenuService.list(wrapper);
    }

    @Log
    @Auth("sysMenu-add")
    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParams({@ApiImplicitParam(name = "SysMenu")})
    @PostMapping("/add")
    public Boolean add(@RequestBody SysMenu param) {
        return sysMenuService.save(param);
    }

    @Log
    @Auth("sysMenu-upd")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @PostMapping("/upd")
    public Boolean upd(@RequestBody SysMenu param) {
        return sysMenuService.updateById(param);
    }

    @Log
    @Auth("sysMenu-sel")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键id")})
    @GetMapping("/get")
    public SysMenu get(Long id) {
        return sysMenuService.getById(id);
    }

    @Log
    @Auth("sysMenu-del")
    @ApiOperation(value = "删除/批量删除", notes = "删除/批量删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = ",分割的id")})
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(@RequestBody JSONObject param) {
        Long id = param.getLong("id");
        return sysMenuService.del(id);
    }

    @Auth("sysMenu-sel")
    @ApiOperation(value = "查询菜单树形列表", notes = "查询菜单树形列表")
    @GetMapping("/getTree")
    public List<Tree<Long>> getTree() {
        return sysMenuService.getTree();
    }
}
